#include<iostream>
#include<fstream>
#include "Person.pb.h"

using namespace PersonalMessage;
int main(int argc, char const *argv[])
{

    GOOGLE_PROTOBUF_VERIFY_VERSION;
    Person person;

    std::fstream input("person.bin", std::ios::in | std::ios::binary);  
    if (!input) {  
        std::cerr << "无法打开文件用于读出" << std::endl;  
        return 1;  
    }  
  
    // 序列化Person对象到文件流  
    if (!person.ParseFromIstream(&input)) {  
        std::cerr << "反序列化到文件失败" << std::endl;  
        return 1;  
    }  
    
    input.close();

    std::cout <<"name:" << person.name() << std::endl;
    std::cout <<"age:" << person.age() << std::endl;
    std::cout <<"address:" << person.address() << std::endl;
    std::cout <<"email:" << person.email() << std::endl;
    for (size_t i = 0; i < person.phones_size(); i++)
    {
        Person::Phone phone = person.phones(i);
        switch (phone.phoentype())
        {
        case Person::PHONE_TYPE_HOME:
            std::cout <<"居家使用的电话号码:" << phone.number() << std::endl;
            break;
        case Person::PHONE_TYPE_WORK:
            std::cout <<"公司使用的电话号码:" << phone.number() << std::endl;
            break;
        default:
            break;
        }
    }
    
    return 0;
}
